package com.zmn.oms.third.yunding.service.impl;

import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.third.yunding.YundingConsts;
import com.zmn.oms.third.yunding.service.YundingService;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 云丁鹿客渠道订单状态消息处理类
 *
 * @author xiewenbing
 * @date 2020/09/10 15:00
 */

@Slf4j
@Service("yundingNotifyService")
public class YundingNotifyServiceImpl implements OrderChangeNotifyService{

    @Autowired
    protected YundingService yundingService;

    @Override
    public void notifyChange(OrderWorkLogChange orderWorkLogChange) {

        // 判断功能是否开启
        if (!YundingConsts.POWER_ON) {
            log.info("【{}】<订单状态同步> 该功能没有开启。", YundingConsts.CHANNEL_NAME);
            return;
        }

        if (!Objects.equals(orderWorkLogChange.getInputType(),OrderConsts.ORDER_INPUT_TYPE_API)) {
            log.info("【{}】的录入方式不为API接入，不需要状态同步", YundingConsts.CHANNEL_NAME);
            return;
        }
        
        log.info("【{}】<订单状态同步> ,状态信息:[{}]。", YundingConsts.CHANNEL_NAME,orderWorkLogChange.toString());

        int logType = orderWorkLogChange.getLogType().intValue();

        try {
            switch (logType) {
                // 确认 - 同步待安排师傅
                /** 不同步
                case OrderLogConsts.ORDER_LOG_TYPE_CONFIRM:
                    yundingServcie.syncOrderConfirm(orderWorkLogChange);
                    break;
                 */
                // 派单 - 同步待师傅联系
                case OrderLogConsts.ORDER_LOG_TYPE_DISTRIBUTE:
                    yundingService.syncOrderDistribute(orderWorkLogChange);
                    break;
                // 修改、改约 - 同步待师傅上门
                case OrderLogConsts.ORDER_LOG_TYPE_UPDATE:
                case OrderLogConsts.ORDER_LOG_TYPE_MASTER_MODIFY_DUTY_TIME:
                    yundingService.syncOrderDutytime(orderWorkLogChange);
                    break;
                // 上门 - 同步待施工
                case OrderLogConsts.ORDER_LOG_TYPE_VISIT:
                    yundingService.syncOrderMasterVisit(orderWorkLogChange);
                    break;
                // 取消、失败、弃单 - 同步取消
                case OrderLogConsts.ORDER_LOG_TYPE_CANCEL:
                case OrderConsts.ORDER_OP_TYPE_FAIL:
                case OrderConsts.ORDER_OP_TYPE_DISCARD:
                    yundingService.syncOrderCancel(orderWorkLogChange);
                    break;
                case OrderLogConsts.ORDER_LOG_TYPE_SERVICE_COMPLETE:
                    yundingService.syncOrderServiceComplete(orderWorkLogChange);
                    break;
                // 完成
                case OrderLogConsts.ORDER_LOG_TYPE_COMPLETE:
                    yundingService.syncOrderComplete(orderWorkLogChange);
                    break;
                default:
                    log.info("【{}】<订单状态-【{}】不同步> ", YundingConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType));
                    return;
            }
        } catch (OmsBaseException exception) {

            log.info("【{}】<订单状态-【{}】同步> 失败 【{}】 ", YundingConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType),
                    exception.getMessage());
        }

        log.info("【{}】<订单状态-【{}】同步> 完成 ", YundingConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType));
    }
}
